package org.game.manager;

import com.dal.gm_api.util.GMError;
import com.google.common.collect.Lists;
import com.sojoys.artifact.manager.JsonManager;
import com.xiaoleilu.hutool.util.ReflectUtil;
import org.error.GMErrorCode;
import org.game.db.StaticResource;

import java.util.List;

/**
 * Created by fxf on 2018-1-16.
 */
public class GMDataManager {

    public static Object getData(String className,String methodName,Object id){
        GMError.isAndTrue(GMErrorCode.CLASS_NAME_INVALID,"类名无效,非getter",StaticResource.cacheMaping.get(className)==null);
        GMError.isAndTrue(GMErrorCode.METHOD_NAME_INVALID,"方法名无效,非getter",!methodName.startsWith("get"));
        Object value = null;
        try {
            value = ReflectUtil.invoke(StaticResource.cacheMaping.get(className),methodName,id);
        }catch (Exception e){
            GMError.isAndTrue(GMErrorCode.PARAM_ERR,"",StaticResource.cacheMaping.get(className)==null);
        }
        Object result ;
        if (value instanceof List){
            List valueList = (List) value;
            List<String> resultList = Lists.newArrayList();
            valueList.forEach(v->resultList.add(JsonManager.me().getJson().toJson(v)));
            result = resultList;
        }else{
            result = JsonManager.me().getJson().toJson(value);
        }
        return result;
    }
}
